package com.nit.bike.mapper.bike;

import com.nit.bike.dto.bike.CardRecordDTO;
import com.nit.bike.dto.system.RoleDTO;
import com.nit.bike.entity.bike.CardRecordEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface CardRecordMapper {
    int deleteByPrimaryKey(Long cardRecordId);

    int insert(CardRecordEntity record);

    int insertSelective(CardRecordEntity record);

    CardRecordEntity selectByPrimaryKey(Long cardRecordId);

    int updateByPrimaryKeySelective(CardRecordEntity record);

    int updateByPrimaryKey(CardRecordEntity record);

    /**
     * 获取表格数据，查询条件自行编写对应代码，需区分模糊和相等，排序条件无需改动
     */
    @Select({"<script>SELECT *, card_record_id AS `key`, r1.station_name AS `begStationName`, " +
            "r2.station_name AS `endStationName`, r1.bike_number, r1.gmt_create AS `begTime`, " +
            "r2.gmt_create AS `endTime` " +
            "FROM basic_card_record cr " +
            "LEFT JOIN basic_ride r1 ON cr.beg_ride_id = r1.ride_id " +
            "LEFT JOIN basic_ride r2 ON cr.end_ride_id = r2.ride_id " +
            "WHERE cr.user_id = #{userId} " +
            "<if test='recordType != null'>AND record_type = #{recordType} </if>" +
            "<if test='stationId != null'>AND (r1.station_id = #{stationId} OR r2.station_id = #{stationId}) </if>" +
            "<if test='bikeId != null'>AND r1.bike_id = #{bikeId}) </if>" +
            "<if test='begDate != null'>AND cr.gmt_create BETWEEN #{begDate} AND #{endDate} </if>" +
            "ORDER BY " +
            "<if test='sortField != null'> ${sortField} ${sortOrder}, </if>" +
            "`key` DESC " +
            "</script>"})
    List<CardRecordDTO> list4Table(
            @Param("userId") Long userId,
            @Param("recordType") String recordType,
            @Param("stationId") String stationId,
            @Param("bikeId") String bikeId,
            @Param("begDate") String begDate,
            @Param("endDate") String endDate,
            @Param("sortField") String sorterField,
            @Param("sortOrder") String sorterOrder) throws Exception;

    /**
     * 获取表格数据，查询条件自行编写对应代码，需区分模糊和相等，排序条件无需改动
     */
    @Select({"<script>SELECT *, card_record_id AS `key`, r1.station_name AS `begStationName`, " +
            "r2.station_name AS `endStationName`, r1.bike_number, r1.gmt_create AS `begTime`, " +
            "r2.gmt_create AS `endTime` " +
            "FROM basic_card_record cr " +
            "LEFT JOIN basic_ride r1 ON cr.beg_ride_id = r1.ride_id " +
            "LEFT JOIN basic_ride r2 ON cr.end_ride_id = r2.ride_id " +
            "WHERE record_type = 0 " +
            "<if test='stationId != null'>AND (r1.station_id = #{stationId} OR r2.station_id = #{stationId}) </if>" +
            "<if test='bikeId != null'>AND r1.bike_id = #{bikeId} </if>" +
            "<if test='begDate != null'>AND cr.gmt_create BETWEEN #{begDate} AND #{endDate} </if>" +
            "ORDER BY " +
            "<if test='sortField != null'> ${sortField} ${sortOrder}, </if>" +
            "`key` DESC " +
            "</script>"})
    List<CardRecordDTO> list4Bike(
            @Param("stationId") String stationId,
            @Param("bikeId") String bikeId,
            @Param("begDate") String begDate,
            @Param("endDate") String endDate,
            @Param("sortField") String sorterField,
            @Param("sortOrder") String sorterOrder) throws Exception;

    /**
     * 插入数据后立刻更新卡号
     */
    @Update("UPDATE basic_card_record cr " +
            "SET card_number = #{cardNumber} " +
            "WHERE card_id = #{cardId} LIMIT 1")
    void pay(
            @Param("cardId") Long cardId,
            @Param("cardNumber") String cardNumber) throws Exception;
}
